package com.shengqi.xinnongren.work.ui.home.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.home.model.ClassBean;
import com.shengqi.xinnongren.work.ui.home.view.HomePage2View;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class HomePage2Presenter extends BasePresenter<HomePage2View> {
    public void getClassList() {
        final JSONObject jsonData = Constants.getJsonData(new HashMap<>(), null);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getClassList(requestBody), s -> {
            HttpRespond<String> httpResult = Global.getHttpResult(s);
            if (httpResult.result == 1) {
                List<ClassBean> list = new Gson().fromJson(httpResult.data, new TypeToken<List<ClassBean>>() {
                }.getType());
                getView().onGetClassListSucceed(list);
            }
        });
    }
}
